using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Sirenix.OdinInspector;
using UnityEngine;


public class VLifeSystem : MonoBehaviour
{
    //思考这个是否需要依赖于实体 , 谁的初始化级别是最高的
    public void Init()
    {

    }
    public static float timeScaleFactor = 3600f; // 现实1s等于游戏系统时间3600s
    private static DateTime startTime = new DateTime(2025, 4, 7, 8, 0, 0); // 默认从早上八点开始

    public static DateTime CurrentDateTime
    {
        get
        {
            float gameSeconds = UnityEngine.Time.time * timeScaleFactor;
            TimeSpan elapsedTime = TimeSpan.FromSeconds(gameSeconds);
            return startTime.Add(elapsedTime);
        }
    }

    public static string TimeStamp
    {
        get
        {
            return CurrentDateTime.ToString("yyyy年M月d日 h:mm tt", CultureInfo.CreateSpecificCulture("zh-CN"));
        }
    }

    public static float GetRealTimeUntil(DateTime targetTime)
    {
        TimeSpan timeDifference = targetTime - CurrentDateTime;
        return (float)timeDifference.TotalSeconds / timeScaleFactor;
    }

    public static DateTime ParseDateTime(string dateTimeString)
    {
        try
        {
            //逆天之HH导致的无端bug, 大写是24小时制,小写是12小时
            var dataT = DateTime.ParseExact(dateTimeString, "yyyy年M月d日 H:mm", CultureInfo.CreateSpecificCulture("zh-CN"));
            return dataT;
        }
        catch (Exception ex)
        {
            Debug.LogError(ex.Message + dateTimeString);
            throw ex;
            //String was not recognized as a valid DateTime.
            //"System.DateTime ParseExact(System.ReadOnlySpan`1[System.Char], System.ReadOnlySpan`1[System.Char], System.Globalization.DateTimeFormatInfo, System.Globalization.DateTimeStyles)"
        }


    }

    public static float GetRealTimeUntil(string dateTimeString) => GetRealTimeUntil(DateTime.ParseExact(dateTimeString, "yyyy年M月d日 h:mm tt", CultureInfo.CreateSpecificCulture("zh-CN")));

}

public static class VLifeCoreExtension
{
    public static Dictionary<VLifeCore, VLifeMemory> globleMemorys;


    public static void Save(this VLifeCore lifeCore, LLMInteractPairsBase interact)
    {
        if (globleMemorys.ContainsKey(lifeCore))
        {
            globleMemorys[lifeCore].AddInteract(interact);
        } 
        else
        {
            var memory = new VLifeMemory();
            memory.AddInteract(interact);
            globleMemorys[lifeCore] = memory;
        }
    }
}

public class VLifeMemory
{
    public Queue<LLMInteractPairsBase> historyInteracts;
    public string memoryCache;
    //public memoeyHanlder persistanceHandler;
    public async Task<string> Simplify()
    {
        //打成一个string
        string total = "";
        int count = 0;
        foreach (var item in historyInteracts)
        {
            var singleInteract = $"tag:{item.tag},userInput:{item.context},yourResponse:{item.response}"; ; //这是一条交互的化简内容，还应该需要对参数有额外的处理
            total += $"{++count}.{singleInteract}\n";
        }
        //不同的交互行为还应该定义简化权重，目前放行
        return await new LLMIP_Simplify() { interactContext = total }.Generate();
    }
    public async void SimplifyWriteIn()
    {
        memoryCache += await Simplify();
        historyInteracts.Clear();
    }
    public void AddInteract(LLMInteractPairsBase interact)
    {
        if (interact == null) return;
        historyInteracts.Enqueue(interact);
        if (historyInteracts.Count >= 5)
            SimplifyWriteIn();
    }
}